package cn.edu.tju.se.records.service.impl;

import java.io.File;
import java.util.List;

import cn.edu.tju.se.base.service.FileManager;
import cn.edu.tju.se.base.service.ReadProperties;
import cn.edu.tju.se.records.dao.PatientDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.tju.se.records.dao.PhotoDao;
import cn.edu.tju.se.records.domain.Patient;
import cn.edu.tju.se.records.domain.Photo;
import cn.edu.tju.se.records.service.PhotoService;

@Service
public class PhotoServiceImpl implements PhotoService{
	@Autowired PhotoDao photoDao;


	@Autowired PatientDao patientDao;

	public void setPatientDao(PatientDao patientDao) {
		this.patientDao = patientDao;
	}

	public void setPhotoDao(PhotoDao photoDao) {
		this.photoDao = photoDao;
	}

	@Override
	public Photo get(int id) {
		return photoDao.get(id);
	}

	@Override
	public void update(Photo photo) {
		photoDao.update(photo);
	}

	@Override
	public void add(Photo photo) {
		photoDao.save(photo);	
	}

	@Override
	public void remove(int id) {
		photoDao.remove(photoDao.get(id));
	}

	@Override
	public List<Photo> getAll() {
		return photoDao.loadAll();
	}
	
	@Override
	public List<Photo> findPhotoByPatientId(String PatientId){
		return photoDao.getPhotoByPatientId(PatientId);
	}

	@Override
	public List<Photo> findPhotoByPhotoDate(String Patientid,String photodate){
		return photoDao.getPhotoByDate(Patientid,photodate);
	}

	@Override
	public void removePhotosbyPatientId(int PatientId){
		String patientid = patientDao.get(PatientId).getPatientid();
		photoDao.removePhotosbyPatientid(patientid);
	}

	@Override
	public void removePhotoFilesbyPatientId(int PatientId){
		String patientid = patientDao.get(PatientId).getPatientid();
//		ReadProperties readproperties = new ReadProperties();
		String imageRoot = photoDao.getPhotoRoot();
		File dispalyDir = new File(imageRoot + "/compress/" + patientid);
		File uploadDir = new File(imageRoot + "/upload/" + patientid);
		FileManager manager = new FileManager();
		if(dispalyDir.exists()){
			manager.deleteAll(dispalyDir);
			dispalyDir.delete();
		}
		if(uploadDir.exists()){
			manager.deleteAll(uploadDir);
			uploadDir.delete();
		}
	}

	public void removePhotosbyPhotoDate(String Patientid,String photodate){
		photoDao.removePhotosbyDate(Patientid,photodate);
	}


	public void removePhotoFilesbyPhotoDate(String Patientid,String photodate){
		String imageRoot = photoDao.getPhotoRoot();
		File dispalyDir = new File(imageRoot + "/compress/" + Patientid+"/"+photodate);
		File uploadDir = new File(imageRoot + "/upload/" + Patientid+"/"+photodate);
		FileManager manager = new FileManager();
		if(dispalyDir.exists()){
			manager.deleteDir(dispalyDir);
		}
		if(uploadDir.exists()){
			manager.deleteDir(uploadDir);
		}
	}

	public String getImgPath(){
		return photoDao.getPhotoRoot();
	}
}
